﻿using System;
using System.Collections.Generic;
using System.Text;
using Common;
using Common.Config;
using Common.Model;
using ConsoleService.Model;

namespace ConsoleService.API
{
    public class UserAPI
    {

        public Login GetUserToken()
        {
            //登录
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Account", "admin");
            parameters.Add("PassWord", "Q1w2e3r4");

            RequestModel request = new RequestModel()
            {
                Method = RestSharp.Method.POST,
                Parameters = parameters,
                Resources = "/api/User/Login",
                WebSide = Configs.WebUrl,
            };
            var user = WebHelper.HttpRequest<Login>(request);
            return user;
        }

        public void WrittenQueue(string Message, string token = "")
        {
            if (string.IsNullOrEmpty(token))
            {
                token = GetUserToken().data.Token;
            }
            WebHelper.HttpRequest($"{Configs.WebUrl}api/User/WrittenQueue?Message=" + Message, token);
        }

        public void WrittenQueue(DelayMessageModel entity, string token = "")
        {
            if (string.IsNullOrEmpty(token))
            {
                token = GetUserToken().data.Token;
            }

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("DelayTime", entity.DelayTime.ToString());
            parameters.Add("CallBackUrl", entity.CallBackUrl);
            parameters.Add("JsonData", entity.JsonData);

            RequestModel request = new RequestModel()
            {
                Token = token,
                Method = RestSharp.Method.POST,
                Parameters = parameters,
                Resources = "/api/User/DelaysQueue",
                WebSide = Configs.WebUrl,
            };
            WebHelper.HttpRequest(request);
        }


        public void RedisPublish(RedisPublishModel entiy)
        {
            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters.Add("Channel", entiy.Channel);
            parameters.Add("Msg", entiy.Msg);

            if (string.IsNullOrEmpty(entiy.Token))
            {
                entiy.Token = GetUserToken().data.Token;
            }

            RequestModel request = new RequestModel()
            {
                Method = RestSharp.Method.POST,
                Parameters = parameters,
                Token = entiy.Token,
                Resources = "/api/Common/RedisPublish",
                WebSide = Configs.WebUrl,
               
            };
            var result = WebHelper.HttpRequest(request);
            Console.WriteLine("调用服务器发布【RedisPublish】：" + result);
        }
    }
}
